﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd.ViewModels;
using enqueteJwVd.Services;
using enqueteJwVd.DAL;


namespace enqueteJwVd.Controllers
{
    [Authorize(Roles = "e-manager")]
    public class SurveyQuestionsController : Controller
    {

        UnitOfWork unitOfWork = new UnitOfWork();
        ISurveyQuestionService surveyQuestionService;
        //private VDLDbEntities db = new VDLDbEntities();

        public SurveyQuestionsController(ISurveyQuestionService surveyQuestionService)
        {
            this.surveyQuestionService = surveyQuestionService;
        }

        public SurveyQuestionsController()
        {
            
        }

        //
        // GET: /SurveyQuestions/

        public ViewResult Index()
        {
            var surveyQuestions = surveyQuestionService.GetAllSurveyQuestions();
            var viewModel = new SurveyQuestionIndexViewModel() { SurveyQuetions = surveyQuestions };
            return View(viewModel);
        }

        //
        // GET: /SurveyQuestions/Details/5

        public ViewResult Details(int? id)
        {
            SurveyQuestion surveyQuestion = unitOfWork.SurveyQuestionRepository.GetById(id);
            return View(surveyQuestion);
        }

        //
        // GET: /SurveyQuestions/Create

        public ActionResult Create()
        {
            ViewBag.GroupFkId = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "Id");
            ViewBag.QuestionTypeFkId = new SelectList(unitOfWork.QuestionTypeRepository.Get(), "Id", "Name");
            return View();
        } 

        //
        // POST: /SurveyQuestions/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")] SurveyQuestion surveyquestion)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.SurveyQuestionRepository.Add(surveyquestion);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");  
            }

            
            ViewBag.QuestionTypeFkId = new SelectList(unitOfWork.QuestionTypeRepository.Get(), "TypeId", "Name", surveyquestion.QuestionTypeFkId);
            return View(surveyquestion);
        }
        
        //
        // GET: /SurveyQuestions/Edit/5
 
        public ActionResult Edit(int? id)
        {
            SurveyQuestion surveyquestion = unitOfWork.SurveyQuestionRepository.GetById(id);
            
            ViewBag.QuestionTypeFkId = new SelectList(unitOfWork.QuestionTypeRepository.Get(), "TypeId", "Name", surveyquestion.QuestionTypeFkId);
            return View(surveyquestion);
        }

        //
        // POST: /SurveyQuestions/Edit/5

        [HttpPost]
        public ActionResult Edit(SurveyQuestion surveyquestion)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.SurveyQuestionRepository.Update(surveyquestion);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");
            }
            
            ViewBag.QuestionTypeFkId = new SelectList(unitOfWork.QuestionTypeRepository.Get(), "TypeId", "Name", surveyquestion.QuestionTypeFkId);
            return View(surveyquestion);
        }

        //
        // GET: /SurveyQuestions/Delete/5
 
        public ActionResult Delete(int? id)
        {
            SurveyQuestion surveyquestion = unitOfWork.SurveyQuestionRepository.GetById(id);
            return View(surveyquestion);
        }

        //
        // POST: /SurveyQuestions/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int? id)
        {
            SurveyQuestion surveyquestion = unitOfWork.SurveyQuestionRepository.GetById(id);
            unitOfWork.SurveyQuestionRepository.Delete(surveyquestion);
            unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            base.Dispose(disposing);
        }


    }
}